package com.shujia.flink.source;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Demo06KafkaSource {
    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // 默认的并行度 等于Kafka Topic分区数
        KafkaSource<String> kafkaSource = KafkaSource
                .<String>builder()
                .setBootstrapServers("master:9092,node1:9092,node2:9092") //kafka集群broker列表
                .setTopics("t2") //指定topic
                .setGroupId("my-group-01") //指定消费者组，一条数据在一个组内只被消费一次
                .setStartingOffsets(OffsetsInitializer.earliest()) //读取数据的位置，earliest：读取所有的数据，latest：读取最新的数据
                .setValueOnlyDeserializer(new SimpleStringSchema()) //反序列的类
                .build();


        DataStreamSource<String> kafkaDS = env.fromSource(kafkaSource, WatermarkStrategy.noWatermarks(), "kafkaSource");

        kafkaDS.print();
        System.out.println("任务的并行度：" + env.getParallelism());

        env.execute();


    }
}
